
# Function for quickly finding questions in codebook ----------------------

find_q <- function(wording,codebook){
  truefalse <- sapply(codebook,function(x)grepl(wording,x$question,T))
  names(codebook)[truefalse]
}

# Function for making factors from codebook -------------------------------


factor_q <- function(var_name,data,codebook){
  if(!is.data.frame(codebook[var_name][[1]]$answer)){
    return(data[,var_name])
  } else {
    return(
      factor(data[,var_name],
             levels = codebook[var_name][[1]]$answer$integer,
             labels = codebook[var_name][[1]]$answer$label)
    )
  }
}

# Function for dummying out variables -------------------------------------

dummy_q <- function(var_name,integers,data){
  variable <- data[,var_name]
  NAs <- is.na(variable)
  new_var <- as.numeric(variable %in% integers)
  new_var[NAs] <- NA
  return(new_var)
}


# Function for looking at object without NAs ------------------------------

no.na <- function(x)x[!is.na(x)]

sumna <- function(x) sum(is.na(x))

# Function to merge in el2 covariates etc. --------------------------------

merge_el2 <- function(varname){
  # Get the el2 varname 
  el2_varname <- paste0(varname,"_el2")
  # Figure out what obs can be merged in
  to_merge <- !is.na(el3[,el2_varname])
  # Check if there is any risk of overwriting new data
  will_overwrite <- any(!is.na(el3[to_merge,varname]))
  if(will_overwrite){
    warning(paste0(
      "You are overwriting ", 
      sum(!is.na(el3[to_merge,varname])),
      " observations from the el3 data with observations from the el2 data."
    ))
  }
  # In the global environment (<<-) put the el2 data into the el3 variable
  el3[to_merge,varname] <<- el3[to_merge,el2_varname]
}


# Quick function for finding complement -----------------------------------

notin <- function(x,y){
  x[! x %in% y]
}


# Function for printing vector with no extra details ----------------------

minprint <- function(x) cat(paste(x,collapse = "\n"))





# Functions to create plots and tables ------------------------------------

prepare_plot_data <- function(
  group_var, 
  outcome_var = "mob_could_happen",
  control_prime,
  treatment_prime, 
  data
){
  
  data$group_var <- data[, group_var]
  data$outcome <- data[, outcome_var]
  
  treatment_label <- paste0("Prime:\n",treatment_prime)
  control_label <- paste0("Prime:\n",control_prime)
  
  plot_dat <- 
    data %>%
    group_by(group_var) %>% 
    summarize(
      n_men = sum(female == 0, na.rm = TRUE),
      n_women = sum(female == 1, na.rm = TRUE),
      men = mean(outcome[female == 0], na.rm = TRUE),
      women = mean(outcome[female == 1], na.rm = TRUE),
      diff = women - men,
      all = mean(outcome, na.rm = TRUE)
    ) %>%
    gather(gender, prop, men:women) %>% 
    mutate(
      type = paste0(gender,group_var),
      x_position = case_when(
        type == "men0" ~ 1,
        type == "men1" ~ 2,
        type == "women0" ~ 3,
        type == "women1" ~ 4
      ),
      colors = case_when(
        type == "women0" ~ "steelblue2",
        type == "women1" ~ "steelblue4",
        type == "men0" ~ "seagreen2",
        type == "men1" ~ "seagreen4"
      ),
      label = case_when(type == "women0" ~ control_label,
                        type == "women1" ~ treatment_label,
                        type == "men0" ~ control_label,
                        type == "men1" ~ treatment_label),
      n = ifelse(gender == "men", n_men, n_women),
      N_label = paste0("N = ",n)
    )
  
  pval_women <- 
    data %>% 
    filter(female == 1) %>% 
    with(.,t.test(outcome[group_var == 1],outcome[group_var == 0])) %>% 
    tidy() %>% pull(p.value)
  
  pval_men <- 
    data %>% 
    filter(female == 0) %>% 
    with(.,t.test(outcome[group_var == 1],outcome[group_var == 0])) %>% 
    tidy() %>% pull(p.value)
  
  fx <- 
    plot_dat %>% 
    group_by(gender) %>% 
    summarize(effect = prop[group_var == 1] - prop[group_var == 0],
              n = sum(n)) %>% 
    ungroup() %>% 
    mutate(
      p_val = case_when(gender == "men" ~ pval_men,
                        gender == "women" ~ pval_women),
      prelab = case_when(gender == "men" ~ paste0("Among men (N = ",n,"): "),
                         gender == "women" ~ paste0("Among women (N = ",n,"): ")),
      stars = case_when(p_val >= .1 ~ "",
                        p_val >= .05 & p_val < .1 ~ "*",
                        p_val >= .01 & p_val < .05 ~ "**",
                        p_val < .01 ~ "***"),
      effect = round(effect,2)*100,
      effect = ifelse(grepl("-",effect),effect,paste0("+",effect)),
      label = paste0(prelab,effect," pp.",stars)
      
    )
  
  plot_dat$grouped_by <- group_var
  fx$grouped_by <- group_var
  
  position_list <- lapply(c("men","women"), function(g){with(plot_dat, x_position[gender == g])})
  color_list <- lapply(c("men","women"), function(g){with(plot_dat, colors[gender == g])}) %>% unlist()
  label_list <- sapply(c("men","women"), function(g){with(fx, label[gender == g])})
  
  return(list(
    fx = fx,
    plot_dat = plot_dat, 
    position_list = position_list, 
    label_list = label_list, 
    color_list = color_list
  ))
  
}

make_plot <- function(
  plot_dat_list, 
  outcome_description =  "% who say this could happen in their village", 
  label_position = .8
){
  list2env(plot_dat_list, envir = environment())
  
  plot_dat %>%
    ggplot(aes(
      y = prop,
      x = x_position,
      fill = colors
    )) +
    geom_bar(position = position_dodge(width = 0.8),
             stat = "identity",
             width = .3) + 
    scale_fill_manual(values = color_list,guide = FALSE) +
    geom_signif(comparisons = position_list,
                annotations = label_list,
                y_position = label_position) +
    geom_signif(comparisons = list(c(1.5,4.5)),y_position = .7) +
    geom_text(aes(y = prop + (label_position/40),label = scales::percent(prop))) +
    # geom_text(aes(y = prop - .02,label = N_label)) +
    scale_y_continuous(labels = scales::percent, 
                       name = outcome_description) +
    scale_x_continuous(breaks = plot_dat$x_position,
                       labels = plot_dat$label,name = "") +
    theme_bw() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.border = element_blank())
}

prepare_one_table_data <- function(
  prime_text,
  prime_var, 
  outcome_var = "mob_could_happen",
  control_prime,
  treatment_prime, 
  data 
){
  
  data$prime <- data[, prime_var]
  data$outcome <- data[, outcome_var]
  
  treatment_label <- treatment_prime
  control_label <- control_prime
  
  model_m <- lm_robust(outcome ~ prime, subset(data, female == 0), se_type = "HC1")
  model_w <- lm_robust(outcome ~ prime, subset(data, female == 1), se_type = "HC1")
  model_w_m_0 <- lm_robust(outcome ~ female, subset(data, prime == 0), se_type = "HC1")
  model_w_m_1 <- lm_robust(outcome ~ female, subset(data, prime == 1), se_type = "HC1")
  model_inter <- lm_robust(outcome ~ female * prime, data, se_type = "HC1")
  
  table_data <- expand.grid(
    female = c(1,0),
    prime = c(0,1)
  )

  table_data$entry <- predict(model_inter, newdata = table_data)
  table_data$entry <- round(table_data$entry,3)*100
  table_data$entry <- paste0(table_data$entry, "\\%")
  
  table_data$row <- c(3,3,4,4)
  table_data$column <- c(2,3,2,3)
  
  table_data$female <- NULL
  table_data$prime <- NULL
  
  fx <- rbind(
    cbind(model_m %>% tidy() %>% filter(term == "prime") %>% select(p.value,estimate), row = 5, column = 3),
    cbind(model_w %>% tidy() %>% filter(term == "prime") %>% select(p.value,estimate), row = 5, column = 2),
    cbind(model_w_m_0 %>% tidy() %>% filter(term == "female") %>% select(p.value,estimate), row = 3, column = 4),
    cbind(model_w_m_1 %>% tidy() %>% filter(term == "female") %>% select(p.value,estimate), row = 4, column = 4),
    cbind(model_inter %>% tidy() %>% filter(term == "female:prime") %>% select(p.value,estimate), row = 5, column = 4)
  ) %>% 
    mutate(
      p_val = p.value,
      stars = case_when(p_val >= .1 ~ "",
                        p_val >= .05 & p_val < .1 ~ "*",
                        p_val >= .01 & p_val < .05 ~ "**",
                        p_val < .01 ~ "***"),
      effect = round(estimate,3)*100,
      effect = ifelse(grepl("-",effect),effect,paste0("+",effect)),
      entry = paste0(effect," pp.",stars)
    ) %>% 
    select(row, column, entry)
    
  # To generate:
  # Data-frame with row and column  
  # ======================================================
  # 1
  # ------------------------------------------------------
  #                     (2,2)         2,3      Gender gap
  # ------------------------------------------------------
  #   3,1                3,2          3,3          3,4             
  #   4,1                4,2          4,3          4,4
  #   ------------------------------------------------------
  #   Effect of prime    5,2          5,3          5,4
  # ======================================================
  # 
  
  N_m <- sum(1 - data$female)
  N_w <- sum(data$female)
  N_control <- sum(1 - data$prime)
  N_treatment <- sum(data$prime)
    
  table_data <- bind_rows(
    table_data,
    fx,
    data.frame(row = 1, column = 1, entry = prime_text),
    data.frame(row = 3, column = 1, entry = paste0(control_label, " (N = ",N_control,")")),
    data.frame(row = 4, column = 1, entry = paste0(treatment_label, " (N = ",N_treatment,")")),
    data.frame(row = 5, column = 1, entry = "\\textit{Estimated prime effect:}"),
    data.frame(row = 2, column = 4, entry = "\\textit{Estimated gender gap:}"),
    data.frame(row = 2, column = 2, entry = paste0("Women (N = ", N_w,")")),
    data.frame(row = 2, column = 3, entry = paste0("Men (N = ", N_m,")"))
  )
  
  
  return(table_data)
  
}


prepare_one_table_data_no_prime <- function(
  outcome_text,
  outcome_var = "mob_could_happen",
  data 
){
  
  data$outcome <- data[, outcome_var] %>% as.vector()
  
  model_w_m <- lm_robust(outcome ~ female, data, se_type = "HC1")
  
  table_data <- expand.grid(
    female = c(1,0)
  )
  table_data$entry <- predict(model_w_m, newdata = table_data)
  table_data$entry <- round(table_data$entry,3)*100
  table_data$entry <- paste0(table_data$entry, "\\%")
  
  table_data$row <- c(3,3)
  table_data$column <- c(2,3)
  
  table_data$female <- NULL
  fx <- rbind(
    cbind(model_w_m %>% tidy() %>% filter(term == "female") %>% select(p.value,estimate), row = 3, column = 4)
  ) %>% 
    mutate(
      p_val = p.value,
      stars = case_when(p_val >= .1 ~ "",
                        p_val >= .05 & p_val < .1 ~ "*",
                        p_val >= .01 & p_val < .05 ~ "**",
                        p_val < .01 ~ "***"),
      effect = round(estimate,3)*100,
      effect = ifelse(grepl("-",effect),effect,paste0("+",effect)),
      entry = paste0(effect," pp.",stars)
    ) %>% 
    select(row, column, entry)
  
  N_m <- sum(1 - data$female)
  N_w <- sum(data$female)
  
  table_data <- bind_rows(
    table_data,
    fx,
    data.frame(row = 1, column = 1, entry = outcome_text),
    data.frame(row = 3, column = 1, entry = "\\% who agree:"),
    data.frame(row = 2, column = 4, entry = "\\textit{Estimated gender gap:}"),
    data.frame(row = 2, column = 2, entry = paste0("Women (N = ", N_w,")")),
    data.frame(row = 2, column = 3, entry = paste0("Men (N = ", N_m,")"))
  )
  
  
  return(table_data)
  
}



prepare_multiple_tables_data <- function(
  prime_texts,
  prime_vars, 
  outcome_var = "mob_could_happen",
  control_primes,
  treatment_primes, 
  data_list
){
  
  table_data_list <- lapply(X = 1:length(prime_texts), function(i){
    one_table_data <- prepare_one_table_data(prime_text = prime_texts[i], prime_var = prime_vars[i], outcome_var = outcome_var,
                           control_prime = control_primes[i], treatment_prime = treatment_primes[i],
                           data = data_list[[i]])
    one_table_data$table_index <- i
    return(one_table_data)
  })
  
  table_data <- do.call(what = rbind, args = table_data_list)
  
  return(table_data)
  
}

prepare_multiple_tables_data_no_prime <- function(
  outcome_vars,
  outcome_texts,
  data_list
){
  
  table_data_list <- lapply(X = 1:length(outcome_texts), function(i){
    one_table_data <- prepare_one_table_data_no_prime(outcome_text = outcome_texts[i],  outcome_var = outcome_vars[i],
                                             data = data_list[[i]])
    one_table_data$table_index <- i
    return(one_table_data)
  })
  
  table_data <- do.call(what = rbind, args = table_data_list)
  
  return(table_data)
  
}

build_table <- function(table_data){
  header <- "\\begin{tabular}{p{5cm}l|cc|c}\n"
  footer <- "\n\\end{tabular}\n"
  
  index <- unique(table_data$table_index)
  
  tabs <- lapply(
    X = index, 
    FUN = function(i){
      
      tab <- 
        table_data %>% 
        filter(table_index == i)
      
      prime <- 
        tab %>% 
        filter(column == 1) %>% 
        filter(row == 1) %>% 
        pull(entry) %>% 
        paste0("\n\\multicolumn{5}{l}{\\textbf{",.,"}} \\\\ \\hline\n")
      
      tmp <- 
        tab %>% filter(row == 2)
      
      col_labs <- 
        paste0(" & & ", 
               tmp %>% filter(column == 2) %>% pull(entry), 
               " &  ",
               tmp %>% filter(column == 3) %>% pull(entry),
               " & ", 
               tmp %>% filter(column == 4) %>% pull(entry),
               "\\\\ \\hline\n"
        )
      tmp <- 
        tab %>% filter(row == 3)  
      
      prime_1 <-       
        paste0(
          tmp %>% filter(column == 1) %>% pull(entry), 
          " & & ",
          tmp %>% filter(column == 2) %>% pull(entry), 
          " &  ",
          tmp %>% filter(column == 3) %>% pull(entry),
          " & ", 
          tmp %>% filter(column == 4) %>% pull(entry),
          "\\\\ \n"
        )
      
      tmp <- 
        tab %>% filter(row == 4)  
      
      prime_2 <-       
        paste0(
          tmp %>% filter(column == 1) %>% pull(entry), 
          " & & ",
          tmp %>% filter(column == 2) %>% pull(entry), 
          " &  ",
          tmp %>% filter(column == 3) %>% pull(entry),
          " & ", 
          tmp %>% filter(column == 4) %>% pull(entry),
          "\\\\ \\hline\n"
        )
      
      tmp <- 
        tab %>% filter(row == 5)  
      
      last_line <-  
        paste0(
          tmp %>% filter(column == 1) %>% pull(entry), 
          " & & ",
          tmp %>% filter(column == 2) %>% pull(entry), 
          " &  ",
          tmp %>% filter(column == 3) %>% pull(entry),
          " & ", 
          tmp %>% filter(column == 4) %>% pull(entry),
          "\\\\ \\hline \\\\\n"
        )
      
      one_tab <- paste0(prime, col_labs, prime_1, prime_2, last_line)
      
      return(one_tab)
    }
  )
  
  tabs <- do.call(what = paste0, args = tabs)
  
  paste0(header, tabs, footer) %>% cat
  
}


build_table_no_prime <- function(table_data){
  header <- "\\begin{tabular}{p{5cm}l|cc|c}\n"
  footer <- "\n\\end{tabular}\n"
  
  index <- unique(table_data$table_index)
  
  tabs <- lapply(
    X = index, 
    FUN = function(i){
      
      tab <- 
        table_data %>% 
        filter(table_index == i)
      
      prime <- 
        tab %>% 
        filter(column == 1) %>% 
        filter(row == 1) %>% 
        pull(entry) %>% 
        paste0("\n\\multicolumn{5}{l}{\\textbf{",.,"}} \\\\ \\hline\n")
      
      tmp <- 
        tab %>% filter(row == 2)
      
      col_labs <- 
        paste0(" & & ", 
               tmp %>% filter(column == 2) %>% pull(entry), 
               " &  ",
               tmp %>% filter(column == 3) %>% pull(entry),
               " & ", 
               tmp %>% filter(column == 4) %>% pull(entry),
               "\\\\ \\hline\n"
        )
      tmp <- 
        tab %>% filter(row == 3)  
      
      prime_1 <-       
        paste0(
          tmp %>% filter(column == 1) %>% pull(entry), 
          " & & ",
          tmp %>% filter(column == 2) %>% pull(entry), 
          " &  ",
          tmp %>% filter(column == 3) %>% pull(entry),
          " & ", 
          tmp %>% filter(column == 4) %>% pull(entry),
          "\\\\ \n"
        )
      
      one_tab <- paste0(prime, col_labs, prime_1)
      
      return(one_tab)
    }
  )
  
  tabs <- do.call(what = paste0, args = tabs)
  
  paste0(header, tabs, footer) %>% cat
  
}


make_gender_plot <- function(data, outcome_var, outcome_description, label_position = .6){
  
  data$outcome <- data %>% pull(!!outcome_var)
  
  plot_dat <- 
    data %>%
    summarize(
      n_men = sum(female == 0, na.rm = TRUE),
      n_women = sum(female == 1, na.rm = TRUE),
      men = mean(outcome[female == 0], na.rm = TRUE),
      women = mean(outcome[female == 1], na.rm = TRUE),
      diff = women - men,
      all = mean(outcome, na.rm = TRUE)
    ) %>%
    gather(gender, prop, men:women) %>% 
    mutate(
      x_position = case_when(
        gender == "men" ~ 1,
        gender == "women" ~ 2
      ),
      colors = case_when(
        gender == "men" ~ "seagreen4",
        gender == "women" ~ "steelblue4"
      ),
      n = ifelse(gender == "men", n_men, n_women),
      N_label = paste0("N = ",n),
      label = case_when(
        gender == "men" ~ paste0("Men (", N_label,")"),
        gender == "women" ~ paste0("Women (", N_label,")"),
      ),
    )
  
  pval <- 
    data %>% 
    with(.,t.test(outcome[female == 1],outcome[female == 0])) %>% 
    tidy() %>% pull(p.value)
  
  fx <- 
    plot_dat %>% 
    summarize(effect = mean(diff), 
              p_val = pval, 
              # prelab = paste0("Difference between men (",
              #                 N_label[gender == "men"],
              #                 ") and women (", 
              #                 N_label[gender == "women"],
              #                 "): "),
              prelab = paste0("Difference (men vs. women): "),
              stars = case_when(p_val >= .1 ~ "",
                                p_val >= .05 & p_val < .1 ~ "*",
                                p_val >= .01 & p_val < .05 ~ "**",
                                p_val < .01 ~ "***"),
              effect = round(effect,2)*100,
              effect = ifelse(grepl("-",effect),effect,paste0("+",effect)),
              label = paste0(prelab,effect," pp.",stars)
              
    )
  
  
  position_list <- lapply(c("men","women"), function(g){with(plot_dat, x_position[gender == g])})
  color_list <- lapply(c("men","women"), function(g){with(plot_dat, colors[gender == g])}) %>% unlist()
  
  plot_dat %>%
    ggplot(aes(
      y = prop,
      x = x_position,
      fill = colors
    )) +
    geom_bar(position = position_dodge(width = 0.8),
             stat = "identity",
             width = .3) + 
    scale_fill_manual(values = color_list,guide = FALSE) +
    geom_signif(comparisons = list(c(1,2)),
                annotations = fx$label,
                y_position = label_position) +
    geom_signif(comparisons = list(c(1.5,4.5)),y_position = .7) +
    geom_text(aes(y = prop + (label_position/40),label = scales::percent(prop))) +
    # geom_text(aes(y = prop - .02,label = N_label)) +
    scale_y_continuous(labels = scales::percent, 
                       name = outcome_description) +
    scale_x_continuous(breaks = plot_dat$x_position,
                       labels = plot_dat$label,name = "") +
    theme_bw() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.border = element_blank())  
  
}




prepare_one_table_dat_no_perc <- function(
  prime_text,
  prime_var, 
  outcome_var = "mob_could_happen",
  control_prime,
  treatment_prime, 
  data 
){
  
  data$prime <- data[, prime_var]
  data$outcome <- data[, outcome_var]
  
  treatment_label <- treatment_prime
  control_label <- control_prime
  
  model_m <- lm_robust(outcome ~ prime, subset(data, female == 0),se_type = "HC1")
  model_w <- lm_robust(outcome ~ prime, subset(data, female == 1),se_type = "HC1")
  model_w_m_0 <- lm_robust(outcome ~ female, subset(data, prime == 0),se_type = "HC1")
  model_w_m_1 <- lm_robust(outcome ~ female, subset(data, prime == 1),se_type = "HC1")
  model_inter <- lm_robust(outcome ~ female * prime, data,se_type = "HC1")
  
  table_data <- expand.grid(
    female = c(1,0),
    prime = c(0,1)
  )

  table_data$entry <- predict(model_inter, newdata = table_data)
  table_data$entry <- round(table_data$entry,3)
  table_data$entry <- paste0(table_data$entry)
  
  table_data$row <- c(3,3,4,4)
  table_data$column <- c(2,3,2,3)
  
  table_data$female <- NULL
  table_data$prime <- NULL
  
  fx <- rbind(
    cbind(model_m %>% tidy() %>% filter(term == "prime") %>% select(p.value,estimate), row = 5, column = 3),
    cbind(model_w %>% tidy() %>% filter(term == "prime") %>% select(p.value,estimate), row = 5, column = 2),
    cbind(model_w_m_0 %>% tidy() %>% filter(term == "female") %>% select(p.value,estimate), row = 3, column = 4),
    cbind(model_w_m_1 %>% tidy() %>% filter(term == "female") %>% select(p.value,estimate), row = 4, column = 4),
    cbind(model_inter %>% tidy() %>% filter(term == "female:prime") %>% select(p.value,estimate), row = 5, column = 4)
  ) %>% 
    mutate(
      p_val = p.value,
      stars = case_when(p_val >= .1 ~ "",
                        p_val >= .05 & p_val < .1 ~ "*",
                        p_val >= .01 & p_val < .05 ~ "**",
                        p_val < .01 ~ "***"),
      effect = round(estimate,3),
      effect = ifelse(grepl("-",effect),effect,paste0("+",effect)),
      entry = paste0(effect,stars)
    ) %>% 
    select(row, column, entry)
  
  # To generate:
  # Data-frame with row and column  
  # ======================================================
  # 1
  # ------------------------------------------------------
  #                     (2,2)         2,3      Gender gap
  # ------------------------------------------------------
  #   3,1                3,2          3,3          3,4             
  #   4,1                4,2          4,3          4,4
  #   ------------------------------------------------------
  #   Effect of prime    5,2          5,3          5,4
  # ======================================================
  # 
  
  N_m <- sum(1 - data$female)
  N_w <- sum(data$female)
  N_control <- sum(1 - data$prime)
  N_treatment <- sum(data$prime)
  
  table_data <- bind_rows(
    table_data,
    fx,
    data.frame(row = 1, column = 1, entry = prime_text),
    data.frame(row = 3, column = 1, entry = paste0(control_label, " (N = ",N_control,")")),
    data.frame(row = 4, column = 1, entry = paste0(treatment_label, " (N = ",N_treatment,")")),
    data.frame(row = 5, column = 1, entry = "\\textit{Estimated prime effect:}"),
    data.frame(row = 2, column = 4, entry = "\\textit{Estimated gender gap:}"),
    data.frame(row = 2, column = 2, entry = paste0("Women (N = ", N_w,")")),
    data.frame(row = 2, column = 3, entry = paste0("Men (N = ", N_m,")"))
  )
  
  
  return(table_data)
  
}




prepare_multiple_tables_data_no_perc <- function(
  prime_texts,
  prime_vars, 
  outcome_var = "mob_could_happen",
  control_primes,
  treatment_primes, 
  data_list
){
  
  table_data_list <- lapply(X = 1:length(prime_texts), function(i){
    one_table_data <- prepare_one_table_dat_no_perc(prime_text = prime_texts[i], prime_var = prime_vars[i], outcome_var = outcome_var,
                                             control_prime = control_primes[i], treatment_prime = treatment_primes[i],
                                             data = data_list[[i]])
    one_table_data$table_index <- i
    return(one_table_data)
  })
  
  table_data <- do.call(what = rbind, args = table_data_list)
  
  return(table_data)
  
}

